package com.meichis.mcsappframework.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.meichis.mcsappframework.R;


public class CustomMessageDialog extends AlertDialog implements OnClickListener {

    private TextView tv_title, tv_message,tv_cancel,tv_ok;
    private ImageView iv_close;
    private String title, message;
    private Spanned spannedMessage = null;
    private SureClickListener mclicklistener;
    private CancelClickListener CancelClick;
    private DeleteClickListener deleteClickListener;
    //有取消事件时，是否可以点空白处去取消弹框
    private boolean isCancelable = false;
    private String sureTxt,cancleTxt;

    public interface SureClickListener {
        void onClick();
    }

    public interface CancelClickListener {
        void onClick();
    }

    public interface DeleteClickListener {
        void onClick();
    }

    public CustomMessageDialog(Context context, String title, String message) {
        super(context);
        this.title = title;
        this.message = message;
    }

    public CustomMessageDialog(Context context, String title, String message, SureClickListener clicklistener) {
        super(context);
        this.title = title;
        this.message = message;
        mclicklistener = clicklistener;
    }

    public CustomMessageDialog(Context context, String title, String message, SureClickListener clicklistener, CancelClickListener cancelClick) {
        super(context);
        this.title = title;
        this.message = message;
        mclicklistener = clicklistener;
        CancelClick = cancelClick;
    }

    public CustomMessageDialog(Context context, String title, String message, String sureTxt, String cancelTxt, SureClickListener clicklistener, CancelClickListener cancelClick) {
        super(context);
        this.title = title;
        this.message = message;
        this.cancleTxt = cancelTxt;
        this.sureTxt = sureTxt;
        mclicklistener = clicklistener;
        CancelClick = cancelClick;
    }

    public CustomMessageDialog(Context context, String title, Spanned message, SureClickListener clicklistener, CancelClickListener cancelClick) {
        super(context);
        this.title = title;
        this.spannedMessage = message;
        mclicklistener = clicklistener;
        CancelClick = cancelClick;
    }

    public CustomMessageDialog(Context context, String title, String message, SureClickListener clicklistener, CancelClickListener cancelClick, DeleteClickListener deleteClickListener) {
        super(context);
        this.title = title;
        this.message = message;
        mclicklistener = clicklistener;
        CancelClick = cancelClick;
        this.deleteClickListener = deleteClickListener;
    }

    public CustomMessageDialog(Context context, String title, String message, boolean isCancelable, SureClickListener clicklistener, CancelClickListener cancelClick) {
        super(context);
        this.title = title;
        this.message = message;
        this.isCancelable = isCancelable;
        mclicklistener = clicklistener;
        CancelClick = cancelClick;
    }

    public CustomMessageDialog(Context context, String title, String message, String cancleTxt, boolean isCancelable, SureClickListener clicklistener, CancelClickListener cancelClick) {
        super(context);
        this.title = title;
        this.message = message;
        mclicklistener = clicklistener;
        this.isCancelable = isCancelable;
        CancelClick = cancelClick;
        this.cancleTxt = cancleTxt;
    }

    @Override
    public void setTitle(CharSequence title) {
        tv_title.setText(title);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        setContentView(R.layout.dialog_my);

        tv_title = findViewById(R.id.tv_title);
        tv_message = findViewById(R.id.tv_message);
        iv_close = findViewById(R.id.iv_delete);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_ok = findViewById(R.id.tv_ok);

        if(spannedMessage != null){
            tv_message.setText(spannedMessage);
        }else {
            tv_message.setText(message);
        }
        if (!title.equals("0")) {
            setTitle(title);
        }
        if (!TextUtils.isEmpty(cancleTxt)){
            tv_cancel.setText(cancleTxt);
        }
        if (!TextUtils.isEmpty(sureTxt)){
            tv_ok.setText(sureTxt);
        }
        if (CancelClick == null) {
            findViewById(R.id.tv_cancel).setVisibility(View.GONE);
            setCancelable(isCancelable);
        } else {
            findViewById(R.id.tv_cancel).setOnClickListener(this);
            setCancelable(isCancelable);
        }

        if(deleteClickListener == null){
            iv_close.setVisibility(View.GONE);
        }
        findViewById(R.id.tv_ok).setOnClickListener(this);
        iv_close.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        dismiss();
        int i = v.getId();
        if (i == R.id.tv_cancel) {
            if (CancelClick != null) {
                CancelClick.onClick();
            }
        } else if (i == R.id.tv_ok) {
            if (mclicklistener != null) {
                mclicklistener.onClick();
            }
        } else if (i == R.id.iv_delete) {
            if (deleteClickListener != null) {
                deleteClickListener.onClick();
            } else {
                dismiss();
            }
        }

    }

}
